<?php
defined('BASEPATH') or exit('No direct script access allowed');
require APPPATH.'libraries/BaseApiController.php';
class teacher extends  BaseApiController
{
   public function index(){

   }
   
   public function list_by_page(){
	   $token=$this->verifyToken();
	   if($token){
			 $keys=[
				 ["name","option","姓名",""],
				 ["mobile","option","电话号码",""],
				 ["address","option","性别",""],
				 ["idcard","option","身份证号码",""],
			 ];
			 
			 $data=$this->getFields($keys);

			 foreach($data as $key=>$value){
				 $this->db->like($key,$value); 
			 }
			 $this->db->where("schcode",$token["schcode"]);
			 $res=$this->db->select("count(*) as count")->get("teacher")->row_array();
			 $numrows=(int)$res["count"];
			 
			 foreach($data as $key=>$value){
				 $this->db->like($key,$value); 
			 }
			 $this->db->where("schcode",$token["schcode"]);
			 $rows=$this->db->get("teacher")->result_array();
			 
			 $this->WRITE("code",1);
			 $this->WRITE("numrows",$numrows);
			 $this->WRITE("list",$rows);
			 $this->RESPONSE();	 
		}
   }
   
   public function list_option(){
	   $token=$this->verifyToken();
	   if($token){
		   $this->db->where("schcode",$token["schcode"]);
		   $rows=$this->db->get("teacher")->result_array();
		   $this->WRITE("code",1);
		   $this->WRITE("list",$rows);
		   $this->RESPONSE();	 
	   }
   }
   
   public function create(){
	   $token=$this->verifyToken();
	   if($token){
		   $keys=[
				 ["name","required","学员姓名",""],
				 ["mobile","required","电话号码",""],
				 ["gender","option","性别","男"],
				 ["idcard","option","身份证号码",""],
				 ["address","option","家庭地址",""],
				 ["memo","option","备注",""],
		   ];
		   $data=$this->getFields($keys);
		   $data["schcode"]=$token["schcode"];
		   $res=$this->db->insert("teacher",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","新增学员成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","新增学员失败!");
			   $this->RESPONSE();	
		   }
	   }
   }
   
   public function update(){
	   $token=$this->verifyToken();
	   if($token){
		   $keys=[
				["tid","required","教师索引",""],
				["name","required","学员姓名",""],
				["mobile","required","电话号码",""],
				["gender","option","性别","男"],
				["idcard","option","身份证号码",""],
				["address","option","家庭地址",""],
				["memo","option","备注",""],
		   ];
		   
		   $data=$this->getFields($keys);
		   
		   $res=$this->db->where("tid",$data["tid"])->update("teacher",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","删除成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","删除失败!");
			   $this->RESPONSE();	
		   }
	   }
   
   }
   
   
   public function delete(){ 
	   $token=$this->verifyToken();
	   if($token){
		   $data=$this->getFields(array(["tid","required","教师索引",""]));
		   $res=$this->db->where("tid",$data["tid"])->delete("teacher",$data);
		   if($res){
			   $this->WRITE("code",1);
			   $this->WRITE("msg","删除教师信息成功!");
			   $this->RESPONSE();	
		   }else{
			   $this->WRITE("code",0);
			   $this->WRITE("msg","删除教师信息失败!");
			   $this->RESPONSE();	
		   }
		}
   }
}